﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace CoTime.Plugins
{
    static class PluginLoader
    {
        internal static List<Plugin> GetPlugins()
        {
            var plugins = new List<Plugin>();

            if (Directory.Exists(Config.PluginsPath))
            { 
                var directoryInfo = new DirectoryInfo(Config.PluginsPath);
            
                DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories("*", SearchOption.TopDirectoryOnly);

                plugins = (from info in directoryInfos
                           let flag = 0
                           let exeInfos = info.GetFiles("*.exe", SearchOption.TopDirectoryOnly)
                           let iconInfo = info.GetFiles("icon.png", SearchOption.TopDirectoryOnly)
                           let descInfo = info.GetFiles("desc.txt", SearchOption.TopDirectoryOnly)
                           where exeInfos.Length == 1 && iconInfo.Length == 1 && descInfo.Length == 1
                           select new Plugin
                                      {
                                          Description = File.ReadAllText(descInfo[0].FullName), IconPath = iconInfo[0].FullName, ExePath = exeInfos[0].FullName
                                      }).ToList();
            }
            return plugins;
        }
    }

    class Plugin
    {
        internal String ExePath { get; set; }
        internal String IconPath { get; set; }
        internal String Description { get; set; }
    }
}
